// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import axios from 'axios'

const NAME = '大华插件'
const URL = CORE.common('zip/大华浏览器播放插件.zip')

let domain, username, password, token

const getAjax = function getAjax({ method, contentType, url, data }) {
  return new Promise(function (resolve, reject) {
    axios({
      method: method || 'get',
      headers: {
        Authorization: `bearer ${token || ''}`,
        'Content-Type': contentType || 'application/json',
      },
      url: `${domain}/evo-apigw${url}`,
      data: data,
    }).then(function (res) {
      [0, '0', 1000].includes(res.data.code) ? resolve(res.data.data) : reject(res.data)
    }).catch(function (err) {
      reject(err)
    })
  })
}

// ============================== 导出工具 ============================== //

export default {
  // —————————————————————————————— 常量 —————————————————————————————— //

  NAME,
  URL,
  LINK: `<a href="${URL}" target="_blank" download="${NAME}">请下载插件，安装后重试</a>`,

  // —————————————————————————————— 登录服务器 —————————————————————————————— //

  async loginServer(config) {
    // 记录配置
    domain = config.domain
    username = config.username
    password = config.password

    const { publicKey } = await getAjax({ url: `/evo-oauth/1.0.0/oauth/public-key?t=${Date.now()}` })
    const ENCRYPT = new window.JSEncrypt() // 加密工具
    ENCRYPT.setPublicKey(publicKey)
    const value = {
      username,
      password: ENCRYPT.encrypt(password),
      code: '',
      grant_type: 'password',
      client_id: 'web_client',
      client_secret: 'web_client',
      public_key: publicKey,
      verifyCodeFlag: 1,
    }
    const resp = await getAjax({
      method: 'post',
      // contentType: 'application/x-www-form-urlencoded',
      url: '/evo-oauth/1.0.0/oauth/extend/token',
      data: JSON.stringify(value),
    })

    // 记录 TOKEN
    token = resp.access_token
  },

  // —————————————————————————————— 播放接口 —————————————————————————————— //

  PLAY_API: {
    /**
     * 获取实时视频RTSP地址
     */
    real(param) {
      return new Promise((resolve, reject) => {
        axios({
          method: 'post',
          headers: { Authorization: `bearer ${token}` },
          url: domain + '/evo-apigw/admin/API/MTS/Video/StartVideo',
          data: param,
        }).then((res) => {
          resolve(res.data.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    /**
     * 获取对讲的RTSP地址
     */
    talk(param) {
      return new Promise((resolve, reject) => {
        axios({
          method: 'post',
          headers: { Authorization: `bearer ${token}` },
          url: domain + '/evo-apigw/admin/API/MTS/Audio/StartTalk',
          data: param,
        }).then(res => {
          resolve(res.data.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    /**
     * 停止对讲
     */
    stopTalk(param) {
      return new Promise((resolve, reject) => {
        axios({
          method: 'post',
          headers: { Authorization: `bearer ${token}` },
          url: domain + '/evo-apigw/admin/API/MTS/Audio/StopTalk',
          data: param,
        }).then(res => {
          resolve(res.data.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    /**
     * 云台方向控制
     */
    setPtzDirection(param) {
      return new Promise((resolve, reject) => {
        axios({
          method: 'post',
          headers: { Authorization: `bearer ${token}` },
          url: domain + '/evo-apigw/admin/API/DMS/Ptz/OperateDirect',
          data: param,
        }).then(res => {
          resolve(res.data)
        }).catch(res => {
          reject(res.errMsg)
        })
      })
    },
    /**
     * 云台镜头控制
     */
    setPtzCamera(param) {
      return new Promise((resolve, reject) => {
        axios({
          method: 'post',
          headers: { Authorization: `bearer ${token}` },
          url: domain + '/evo-apigw/admin/API/DMS/Ptz/OperateCamera',
          data: param,
        }).then(res => {
          resolve(res.data)
        }).catch(res => {
          reject(res.errMsg)
        })
      })
    },
  },

  // —————————————————————————————— 回放接口 —————————————————————————————— //

  REPLAY_API: {
    /**
     * 通过时间获取录像回放
     */
    playbackByTime(param) {
      return new Promise((resolve, reject) => {
        axios({
          method: 'post',
          headers: { Authorization: `bearer ${token}` },
          url: domain + '/evo-apigw/admin/API/SS/Playback/StartPlaybackByTime',
          data: param,
        }).then(res => {
          resolve(res.data.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    /**
     * 通过文件获取录像回放
     */
    playbackByFile(param) {
      return new Promise((resolve, reject) => {
        axios({
          method: 'post',
          headers: { Authorization: `bearer ${token}` },
          url: domain + '/evo-apigw/admin/API/SS/Playback/StartPlaybackByFile',
          data: param,
        }).then(res => {
          resolve(res.data.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    /**
     * 查询录像
     */
    queryRecord(param) {
      return new Promise((resolve, reject) => {
        axios({
          method: 'post',
          headers: { Authorization: `bearer ${token}` },
          url: domain + '/evo-apigw/admin/API/SS/Record/QueryRecords',
          data: param,
        }).then(res => {
          resolve(res.data.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
  },
}
